<?php
tsLoad('/manage/busi/SysAlbumBusi.class.php');
tsLoad('/manage/busi/SysInfoMenuBusi.class.php');
tsLoad('/manage/busi/SysBoardMenuBusi.class.php');

class SysMenuBusi {
	/**
	 * 添加一个菜单
	 */
	public function addMenu($condition){	
	    global $mysqlTools;
	    //生成唯一ID
	    $uniqid = getCurrentId();
	    
	    //参数处理注入SQL安全问题
	    $condition = $mysqlTools->check_input($condition); 
	    if(empty($condition['menuUrl']))$condition['menuUrl']='null';
	    if(empty($condition['menuOrder']))$condition['menuOrder']='null';
	    try {
	        $mysqlTools->startTransaction();
	        $result = $mysqlTools->executeSql("
                INSERT INTO sys_menu(
                	menuId,
                	menuName,
                	menuUrl,
                	menuDesc,
                	menuParentId,
                	menuIsUsed,
	                menuOrder
                ) VALUES(
                	$uniqid,
	                $condition[menuName],
	                $condition[menuUrl],
	                $condition[menuDesc],
	                $condition[menuParentId],
	                1,
	                $condition[menuOrder]
                )
	        ");
	        
	        $mysqlTools->commitTransaction();
	    } catch (\Exception $e) {
	        $mysqlTools->rollback();
	        return "数据库插入发生异常";
	    }
	    return $result;
	}
	
	/**
	 * 添加一个角色菜单信息
	 */
	public function addRoleMenus($condition){
		global $mysqlTools;
		 
		//参数处理注入SQL安全问题
		//$condition = $mysqlTools->check_input($condition);
		try {
			$mysqlTools->startTransaction();
			//首先删除以前的角色菜单信息
			$result = $mysqlTools->executeSql("
				delete from sys_role_menu where roleId = $condition[roleId];
			");
			//取出角色菜单数组后进行循环插入
			if(!empty($condition['roleMenuIds'])){
				$menuIds = explode(",",$condition['roleMenuIds']);
				foreach ($menuIds as $menuId) {
					if(is_numeric($menuId)){
						$result = $result&&$mysqlTools->executeSql("insert into sys_role_menu(roleId,menuId) values($condition[roleId],$menuId);");
					}
				}
			}
		    
			if($result)
				$mysqlTools->commitTransaction();
			else 
				$mysqlTools->rollback();
		} catch (\Exception $e) {
			$mysqlTools->rollback();
			return "数据库插入发生异常";
		}
		return $result;
	}
	
	/**
	 * 添加一个用户菜单信息
	 */
	public function addUserMenus($condition){
		global $mysqlTools;
			
		//参数处理注入SQL安全问题
		//$condition = $mysqlTools->check_input($condition);
		try {
			$mysqlTools->startTransaction();
			//首先删除以前的角色菜单信息
			$result = $mysqlTools->executeSql("
					delete from sys_user_menu where userId = $condition[userId];
					");
			//取出角色菜单数组后进行循环插入
			if(!empty($condition['userMenuIds'])){
				$menuIds = explode(",",$condition['userMenuIds']);
				foreach ($menuIds as $menuId) {
					if(is_numeric($menuId)){
						$result = $result&&$mysqlTools->executeSql("insert into sys_user_menu(userId,menuId) values($condition[userId],$menuId);");
					}
				}
			}
	
			if($result)
				$mysqlTools->commitTransaction();
			else
				$mysqlTools->rollback();
		} catch (\Exception $e) {
			$mysqlTools->rollback();
			return "数据库插入发生异常";
		}
		return $result;
	}
	
	/**
	 * 修改一个菜单
	 */
	public function updateMenu($condition){
		global $mysqlTools;
		 
		//参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input($condition);
		try {
			$sql = "UPDATE sys_menu SET menuId = menuId";
			if(!empty($condition['menuName'])){
				$sql.= ",menuName = $condition[menuName]";
			}
			if(!empty($condition['menuOrder'])){
				$sql.= ",menuOrder = $condition[menuOrder]";
			}
			if(!empty($condition['menuUrl'])){
				$sql.= ",menuUrl = $condition[menuUrl]";
			}
			if(!empty($condition['menuDesc'])){
				$sql.= ",menuDesc = $condition[menuDesc]";
			}
			if(!empty($condition['menuIsUsed'])){
				$sql.= ",menuIsUsed = $condition[menuIsUsed]";
			}
			$sql.=" where menuId = $condition[menuId]";
			
			$mysqlTools->startTransaction();
			$result = $mysqlTools->executeSql($sql);
			$mysqlTools->commitTransaction();
		}catch (\Exception $e) {
			$mysqlTools->rollback();
			return "数据库插入发生异常";
		}
		return $result;
	}
	
	/**
	 * 删除一个菜单
	 */
	public function delMenu($condition){
	    global $mysqlTools;
	     
	    //参数处理注入SQL安全问题
	    $condition = $mysqlTools->check_input($condition);
	    try {
	        $mysqlTools->startTransaction();
	        $result = $mysqlTools->executeSql("
	           UPDATE sys_menu SET menuIsUsed=2 where menuId = $condition[menuId];
	        ");
	        //查找该菜单有孩子菜单没如果有就递归删除掉
	        $menus = $mysqlTools->selectSql("
	        	SELECT
					sm.menuId,
					sm.menuName,
					sm.menuOrder,
					sm.menuUrl,
					sm.menuDesc,
					sm.menuParentId,
					sm.menuIsUsed,
					sm.menuIsActive,
					sm.menuIsSingle
				FROM
					sys_menu sm
				WHERE
					sm.menuParentId = $condition[menuId]
	       	");
	        
	        if(count($menus)>0){
	        	foreach ($menus as $menu){
	        		$this->delMenu($menu);
	        	}
	        }
	                 
	        $mysqlTools->commitTransaction();
    	} catch (\Exception $e) {
    	   $mysqlTools->rollback();
           return "数据库插入发生异常";
    	}
		return $result;
	}
	
	/**
	 * 查询菜单所有信息
	 * @return Ambigous <multitype:, unknown, mixed, multitype:Ambigous <multitype:, mixed, unknown, multitype:string Ambigous <multitype:, mixed, unknown> > >
	 */
	public function queryMenus(){
		global $mysqlTools;
		//查询用户相册信息
		$children = $mysqlTools->selectSql("
				SELECT
					sm.menuId as id,
					sm.menuName,
					sm.menuOrder,
					sm.menuUrl,
					sm.menuDesc as name,
					sm.menuParentId as pId,
					sm.menuIsUsed,
					sm.menuIsActive,
					sm.menuIsSingle
				FROM
					sys_menu sm
				where 
					sm.menuIsUsed=1
				order by sm.menuOrder asc
		");
	
		return $children;
	}
	
	/**
	 * 查询单个菜单信息
	 */
	public function queryMenuOne($condition){
		global $mysqlTools;
		//查询用户相册信息
		$menu = $mysqlTools->selectSql("
				SELECT
					sm.menuId as id,
					sm.menuName,
					sm.menuOrder,
					sm.menuUrl,
					sm.menuDesc,
					sm.menuParentId as pId,
					sm.menuIsUsed,
					sm.menuIsActive,
					sm.menuIsSingle
				FROM
					sys_menu sm
				where
					sm.menuIsUsed=1 and
					sm.menuId=$condition[menuId]
		");
	
		return $menu;
	}
	
	//通过父亲菜单ID查询菜单信息
	public function queryMenusRoot(){
	    global $mysqlTools,$userSession;
	    $userId = $userSession->getUserSession()['userId'];
	    //查询用户相册信息
	    $children = $mysqlTools->selectSql("
	            SELECT
                	sm.menuId,
                	sm.menuName,
	                sm.menuUrl,
                	sm.menuDesc,
                	sm.menuParentId,
                	sm.menuIsUsed,
                	sm.menuIsActive,
                	sm.menuIsSingle,
	                sm.menuType
                FROM
                	(select sm.* from sys_menu sm where sm.menuId in (
	                   select distinct srm.menuId from sys_role_menu srm 
                       where srm.roleId in (select sur.roleId from sys_user_role sur where sur.userId= $userId)
	                   union 
                        select distinct sum.menuId from sys_user_menu sum where sum.userId = $userId
	                   )
	                ) sm
                WHERE
                	sm.menuParentId = 0
	            ");
	    if(count($children)>0){
            foreach ($children as $i => $child) {
                $children[$i]['menuChildren'] = $this->findChildrenByParentId($child['menuId']);
                
            }
	    }
	
	    return $children;
	}
	
	//递归找孩子
	public function findChildrenByParentId($menuId){
	    global $mysqlTools;	    
	    //查询用户相册信息
	    $children = $mysqlTools->selectSql("
	            SELECT
		            sm.menuId as id,
		            sm.menuName,
		            sm.menuUrl,
		            sm.menuDesc as text,
		            sm.menuParentId,
		            sm.menuIsUsed,
		            sm.menuIsActive,
		            sm.menuIsSingle
	            FROM
	            	sys_menu sm
	            WHERE
	            	sm.menuParentId = $menuId
	    ");
	    $arr=array();
	    if(count($children)>0){
    	    foreach ($children as $i => $child) {
    	       //echo 'parentId:'.$menu[menuId].':'.$i.':count:'.count($children).'<br/>';
    	        
    	       $child['children'] = $this->findChildrenByParentId($child['id']);
    	       
    	    }
    	    return $children;
	    }
	}
	
	/**
	 * 查询角色菜单列表树型
	 */
	public function queryRoleMenuList ($condition)
	{
		global $mysqlTools;
	
		$condition = $mysqlTools->check_input($condition);
	
		$sql = "
			SELECT
				sm.menuId as id,
				sm.menuName,
				sm.menuOrder,
				sm.menuUrl,
				sm.menuDesc as name,
				sm.menuParentId as pId,
				sm.menuIsUsed,
				sm.menuIsActive,
				sm.menuIsSingle,
				CASE WHEN (rm.roleId is not null) THEN 'true' ELSE 'false' END as checked
			FROM
				sys_menu sm left join (select menuId,roleId from sys_role_menu where roleId=$condition[roleId]) rm on sm.menuId=rm.menuId
				where sm.menuIsUsed=1
				order by sm.menuOrder asc
		";
		$list = $mysqlTools->selectSql($sql);
		return $list;
	}
	
	/**
	 * 查询用户菜单列表树型
	 */
	public function queryUserMenuList ($condition)
	{
		global $mysqlTools;
	
		$condition = $mysqlTools->check_input($condition);
	
		$sql = "
			SELECT
				sm.menuId as id,
				sm.menuName,
				sm.menuOrder,
				sm.menuUrl,
				sm.menuDesc as name,
				sm.menuParentId as pId,
				sm.menuIsUsed,
				sm.menuIsActive,
				sm.menuIsSingle,
				CASE WHEN (sum.userId is not null) THEN 'true' ELSE 'false' END as checked
			FROM
				sys_menu sm left join (select menuId,userId from sys_user_menu where userId=$condition[userId]) sum on sm.menuId=sum.menuId
				where sm.menuIsUsed=1
				order by sm.menuOrder asc
		";
		$list = $mysqlTools->selectSql($sql);
		return $list;
	}
}

?>